	package com.example.esaclient;

	import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;



	public class CrossPostingFB extends AsyncTask<Object, Integer, Boolean> {
	 

		private  String URL;
		private  static final String METHOD_NAME = "/CrossPostingFB?";	
		Activity caller;
		
		
		public CrossPostingFB(Activity callerActivity){
			caller=callerActivity;
		}
		
		@Override
		protected Boolean doInBackground(Object... params) {
		
		    	 try {
		    		 Log.w("ESA","Calling CrossPostingTW...");
					 GlobalSettings gs = new GlobalSettings(caller);
					 
					 
					 
					 URL = "http://"+gs.getPrefString(GlobalSettings.PREF_ESA_URL)+"/ServiceRest.svc";			 
					 String url =URL+METHOD_NAME+"id="+params[0]+"&message="+params[1];
							  
					 HttpClient client = new DefaultHttpClient();
				        HttpGet httpGet = new HttpGet(url);
				        try {
				            HttpResponse response = client.execute(httpGet); 	
				            HttpEntity entity = response.getEntity();
				             
				            if(entity != null) {
				                InputStream stream = entity.getContent();
				                String responseString = GlobalSettings.convertStreamToString(stream);
				                
				                
				                   Wrapper w = new Gson().fromJson(responseString, Wrapper.class);
				                   return w.CrossPostingFBResult;
				                   
				            }
				             
				        } catch (ClientProtocolException e) {
				            e.printStackTrace();
				          
				        } catch (IOException e) {
				            e.printStackTrace();		            
				        }
			      }
			     catch (final Exception e) {
			         e.printStackTrace();   
			         
			     }
				
			        
		    
		return false;
		}
		
		  public class Wrapper {
		    	 public Boolean CrossPostingFBResult;
		     }

		
		

		     protected void onPostExecute(Boolean result) {
		    	 
		      	 Log.w("ESA","Exiting CrossPostingFB with result->"+result);
		    	 
		    	 Toast.makeText(caller, "Posted on Facebook!", Toast.LENGTH_SHORT).show();
			    	 
		     }

		
		
	}
